package org.example.tcp.client;

import org.example.common.MyConverter;
import org.example.tcp.TcpData;
import org.example.tcp.server.clientRunable;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    public static void main(String[] args) throws IOException {
        InetAddress address = InetAddress.getByName("192.168.127.1");
        Socket socket = new Socket(address,9999);
        OutputStream outputStream = socket.getOutputStream();
        clientRunable clientRunable = new clientRunable(socket);
        Thread thread = new Thread(clientRunable);
        thread.start();
        System.out.println("退出请输入‘q’");
        Scanner scanner = new Scanner(System.in);
        String name = "王宁";
        String information = name+":"+socket.getPort();
        outputStream.write(information.getBytes());
        while(true){
            String message = scanner.next();
            if (message.equals("q")) {
                TcpData data = new TcpData(name, "down", message,socket.getLocalAddress(), socket.getLocalPort());
                outputStream.write(MyConverter.toByteArray(data));
                break;
            }
            TcpData data = new TcpData(name, "李浩伟", message,socket.getLocalAddress(), socket.getLocalPort());
            outputStream.write(MyConverter.toByteArray(data));
        }
        socket.close();
    }
}
